<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
class Index extends Base{
    public function index(){
        //获取轮播
        $this->assign('banner',$this->formatConfig('web_swiper_index')['value']);

        //获取人物介绍
        $this->assign('person',$this->formatConfig('web_diy_person')['value']);

        //获取常见问题解答
        $this->assign('help',$this->formatConfig('web_diy_help')['value']);

        //获取我们的文化
        $this->assign('cultureImg',$this->formatConfig('web_diy_culture_img')['value']);

        //获取排序前三的产品
        $productData = Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where('a.a_show','1')
        ->where('b.a_show','1')
        ->where('b.type','goods')
        ->order('a.sort', 'desc')
        ->page(1,3)
        ->select()
        ->toArray(); 
        $this->assign('productData',$productData);

        $this->getPageData(1);//获取页面配置信息

        return $this->fetch();
    }

    //产品
    public function product($type_id=0,$page=1){
        $limit = 9;
        $type = $type_id ? ['a.type_id' => $type_id]:[];

        //公共查询条件
        $where = ['a.a_show'=>'1','b.a_show'=>'1','b.type'=>'goods',];
    
        //查询商品 商品表跟 分类表关联，必须是 商品表的a_show =1 且 分类表的a_show =1
        $this->assign('productData',Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where($type)
        ->order('a.id', 'desc')
        ->page($page,$limit)
        ->select()
        ->toArray());
  
        //获取总数
        $this->assign('count',Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->where($where)
        ->where($type)
        ->count());

        //获取分类
        $this->assign('typeData',Db::table('an_type')
        ->where('a_show','1')
        ->where('type','goods')
        ->order('sort','desc')
        ->select()
        ->toArray());

        $this->getPageData(2);
        $this->assign('type',$type_id);
        $this->assign('page',$page);
        $this->assign('limit',$limit);
        return $this->fetch();
    }

    //产品详情
    public function product_details($id,$type_id=0){
        $type = $type_id ? ['a.type_id' => $type_id]:[];

        //公共查询条件
        $where = [ 'a.a_show'=>'1', 'b.a_show'=>'1','b.type'=>'goods',];

        $this->assign('pageContent',Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where('a.id',$id)
        ->find());

        //上一条数据
        $shangID = Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where($type)
        ->where('a.id','>',$id)
        ->order('a.id', 'desc')
        ->min('a.id',false);
        $this->assign('shang',Db::table('an_goods')
            ->where('id',$shangID)
            ->findOrEmpty());

        //下一篇数据
        $xiaID = Db::table('an_goods')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where($type)
        ->where('a.id','<',$id)
        ->order('a.id', 'desc')
        ->max('a.id',false);
        $this->assign('xia',Db::table('an_goods')
            ->where('id',$xiaID)
            ->findOrEmpty());


        $this->getPageData(2);
        $this->assign('type',$type_id);
        return $this->fetch();
    }

    //新闻
    public function news($type_id=0,$page=1){
        $limit = 9;
        $type = $type_id ? ['a.type_id' => $type_id]:[];

        //公共查询条件
        $where = ['a.a_show'=>'1','b.a_show'=>'1','b.type'=>'news',];
        
        //获取新闻 新闻表跟 分类表关联，必须是 新闻表的a_show =1 且 分类表的a_show =1
        $this->assign('newsData',Db::table('an_news')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where($type)
        ->order('a.id', 'desc')
        ->page($page,$limit)
        ->select()
        ->toArray());

        //获取总数
        $this->assign('count',Db::table('an_news')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->where($where)
        ->where($type)
        ->count());

        // 获取分类
        $this->assign('typeData',Db::table('an_type')        
        ->where('a_show','1')
        ->where('type','news')
        ->order('sort','desc')
        ->select()
        ->toArray());

        $this->getPageData(5);
        $this->assign('type',$type_id);
        $this->assign('page',$page);
        $this->assign('limit',$limit);
        return $this->fetch();
    }

    //新闻详情
    public function news_details($id=0,$type_id=0){
        $type = $type_id ? ['a.type_id' => $type_id]:[];

        //公共查询条件
        $where = ['a.a_show'=>'1','b.a_show'=>'1','b.type'=>'news',];

        $this->assign('pageContent',Db::table('an_news')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where('a.id',$id)
        ->find());

        //上一条数据
        $shangID = Db::table('an_news')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name')
        ->where($where)
        ->where($type)
        ->where('a.id','>',$id)
        ->order('a.id', 'desc')
        ->min('a.id',false);
        $this->assign('shang',Db::table('an_news')
            ->where('id',$shangID)
            ->findOrEmpty());

        //下一篇数据
        $xiaID = Db::table('an_news')
        ->alias('a')
        ->join(['an_type'=>'b'],'a.type_id = b.id')
        ->field('a.*,b.name,b.id')
        ->where($where)
        ->where($type)
        ->where('a.id','<',$id)
        ->order('a.id', 'desc')
        ->max('a.id',false);
        $this->assign('xia',Db::table('an_news')
            ->where('id',$xiaID)
            ->findOrEmpty());

        $this->getPageData(5);
        $this->assign('type',$type_id);
        return $this->fetch();
    }

    public function contact(){
        $this->getPageData(8);

        $param = input('post.');
        if(count($param)>0){

            //限制ip 60秒内 只能留言2次
            $res = $this->getResult(2,60);
            if (empty($res)) {
                $this->info['code']=400;
                $this->info['tips']='留言太快，请60秒之后再试';
                return json($this->info);
            }

            $bool = Db::table('an_form')->insert([
                'create_time'=>time(),
                'name'=>isset($param['name'])?$param['name']:'',
                'phone'=>isset($param['phone'])?$param['phone']:'',
                'email'=>isset($param['email'])?$param['email']:'',
                'age'=>isset($param['age'])?$param['age']:'',
                'sex'=>isset($param['sex'])?$param['sex']:'',
                'notes'=>isset($param['notes'])?$param['notes']:'',
                'client'=>isset($param['client'])?$param['client']:'',
                'addr'=>isset($param['addr'])?$param['addr']:'',
                'other'=>isset($param['other'])?json_encode($param['other']):'',
            ]);
            if($bool == 1){
                $this->info['msg'] = '操作成功';
            }else{
                $this->info['code'] = 201;
                $this->info['msg'] = '操作失败';
            }
            return json($this->info);
        }
        return $this->fetch();
    }

    public function about(){
        $this->getPageData(7);
        return $this->fetch();
    }
    
    public function pricing(){
        $this->getPageData(15);
        return $this->fetch();
    }
    
    public function service(){
        $this->getPageData(13);
        return $this->fetch();
    }

    public function testimonial(){
        //获取人物介绍
        $this->assign('person',$this->formatConfig('web_diy_person')['value']);

        $this->getPageData(17);
        return $this->fetch();
    }

    public function error_page(){
        $this->getPageData(16);
        return $this->fetch();
    }
}
